/**
 * @author James Simpson, http://www.jsblogstop.com
 */
if($("#js-post-template").length <= 0) {
  document.write("<div id='js-post-template' class='js-post js-post-template'><div class='js-post-thumb'><img/></div><h3><a></a></h3>");
  document.write("<div class='js-post-body'></div><h4></h4><div class='clear'></div></div>");
}

$("head").append("<link id='js-styles' rel='stylesheet' type='text/css' href='http://jsblogstop-blogger-feed-tools.googlecode.com/svn/suite/jsLabelFeeds.css'/>");
checkStyles();

function checkStyles() {
  if($("#js-post-template").css("display") != "none") {
    setTimeout(checkStyles, 100);
  }
  else {
    $(".js-post").show();
    $("#js-post-template").hide();
  }
} 
function jsPostClickNav(event) {
 var strLink = event.data.href;

 if(strLink.search(document.domain) == -1 && strLink.indexOf("http:") != -1) {
   window.open(strLink,'_blank');
 }
 else {
   window.top.location = strLink;
 }
}
function getUniqueCont() {
 var d = new Date();
 var nRand = Math.floor(Math.random() * d.getTime());
 document.write("<div id='js-label-posts" + nRand + "' class='js-posts-container'></div>");
 return "js-label-posts" + nRand;
}

function jsPostLinks() {
  this.strTitle = "";
  this.strCont = getUniqueCont();
  this.nPosts = 5;
  var This = this;
  
  this.setTitle = function(Title) {
    $("#" + This.strCont + " h2").remove();
    if(Title != "") {
      This.strTitle = Title;
      $("#" + This.strCont).prepend("<h2>" + This.strTitle + "</h2>");
    }
  }
  this.getRelatedLinks = function (nPostLinks)
  {
    var strLabel = "";
    if($("#comments").length > 0)
    {
      var $Labels = $(".post-labels a");
      if($Labels.length > 0)
      {
        $("#" + This.strCont).insertAfter(".post-body");
        $Labels.each(function () { strLabel += $(this).text() + ","; } );	  
	    This.getLabelLinks(strLabel, nPostLinks);
      }
    }
  }
  this.getExtLabelLinks = function (strDomain, strLabel, nPostLinks){
    This.nPosts = nPostLinks;
    $("#" + This.strCont).append("<div class='js-posts-loading'>Loading...</div>");

    var qs = {};
    if(strLabel){
      qs.category = strLabel;
    }
    var strURL = "http://" + strDomain + "/feeds/posts/summary/?alt=json&max-results=50&callback=?";
    $.getJSON(strURL, qs, this.processLabelLinks);
  }
  this.getLabelLinks = function (strLabel, nPostLinks){
  	This.nPosts = nPostLinks;
    $("#" + This.strCont).append("<div class='js-posts-loading'>Loading...</div>");

	var qs = {};
	var strURL = "http://" + document.domain + "/feeds/posts/summary/?category=" + strLabel + "&alt=json&max-results=50";
    $.getJSON(strURL, qs, this.processLabelLinks);
  }
  this.processLabelLinks = function (json) {
    if (json.feed.entry != undefined) {
	  $("#" + This.strCont + " .js-posts-loading").remove();
     $("#" + This.strCont).append("<ul class='js-links-list'></ul>");
      for(var i=0; i < json.feed.entry.length; i++) {
  	  This.renderLink(json.feed.entry[i]);
	    if ((i+1) >= This.nPosts) {
	      break;
	    }
     }
     if($("#" + This.strCont + " li").length > 0) {
       $("#" + This.strCont).show();     
     }
   }
   else {
    $("#" + This.strCont).html("<h3>No Results Found</h3>");
   }
   // $("#" + This.strCont).append("<div class='js-attribution'>powered by <a href='http://www.jsblogstop.com/2011/04/blogger-label-summary-with-thumbnails.html'>JSBlogStop</a></div>");
  }
  this.renderLink = function(Entry) {
  	var htmlLink = "";    
	
	for(var i=Entry.link.length-1;i >= 0; i--) {
      if(Entry.link[i].rel == "alternate" && document.location != Entry.link[i].href) {
	  	htmlLink += "<li><a href='" + Entry.link[i].href + "'>" + Entry.title.$t + "</a></li>";
		  $("#" + This.strCont + " .js-links-list").append(htmlLink);        
	     break;
	   }
    }
  }
}

function jsPostThumbs() {
  this.strTitle = "";
  this.strLayout = "";
  this.strCont = getUniqueCont();
  this.nPosts = 3;
  this.nMaxLength = 0;
  var This = this;
    
  this.setTitle = function(Title) {
    $("#" + This.strCont + " h2").remove();
    if(Title != "") {
      This.strTitle = Title;
      $("#" + This.strCont).prepend("<h2>" + This.strTitle + "</h2>");
    }
  }
  this.getRelatedLinks = function (strTLoc, nPostThumbs, nPostChars)
  {
    var strLabel = "";

    if($("#comments").length > 0)
    {
      var $Labels = $(".post-labels a");
      if($Labels.length > 0)
      {
        $("#" + This.strCont).insertAfter(".post-body");
        $Labels.each(function () { strLabel += $(this).text() + ","; } );	  
	    This.getLabelPost(strLabel, strTLoc, nPostThumbs, nPostChars);
      }
    }
  }
  this.getExtLabelPost = function (strDomain, strLabel, strTLoc, nPostThumbs, nPostChars){
   This.nPosts = nPostThumbs;
   This.nMaxLength = nPostChars;
   This.strLayout = strTLoc;
   This.setTitle(This.strTitle);
   
   $("#" + This.strCont).append("<div class='js-posts-loading'>Loading...</div>");
   var qs = {};
   if(strLabel){
      qs.category = strLabel;
    }
	var strURL = "http://" + strDomain + "/feeds/posts/default/?alt=json&max-results=50&callback=?";
    $.getJSON(strURL, qs, this.processLabelPost);
  }
  this.getLabelPost = function (strLabel, strTLoc, nPostThumbs, nPostChars){
  	This.nPosts = nPostThumbs;
	This.nMaxLength = nPostChars;
	This.strLayout = strTLoc;
	var qs = {};
	var strURL = "http://" + document.domain + "/feeds/posts/default/?category=" + strLabel + "&alt=json&max-results=50";
    $.getJSON(strURL, qs, this.processLabelPost);
  }

  this.processLabelPost = function (json) {
    if (json.feed.entry != undefined) {
	  $("#" + This.strCont + " .js-posts-loading").remove();
      for(var i=0; i < json.feed.entry.length; i++) {
  	    This.renderPost(i, json.feed.entry[i], This.strCont);
	    if ((i+1) >= This.nPosts) {
	      break;
	    }
     }
     if($("#" + This.strCont + " .js-post").length > 0) {
       $("#" + This.strCont).show();     
     }   
   }
   else {
    $("#" + This.strCont).html("<h3>No Results Found</h3>");
   }
   $("#" + This.strCont).append("<div class='js-attribution'>powered by <a href='http://www.jsblogstop.com/2011/04/blogger-label-summary-with-thumbnails.html'>JSBlogStop</a></div>");
  }
  this.renderPost = function (nPost, Entry, strCont)
  {
   var divPost = $("#js-post-template").clone();
   var idPost = "#" + strCont + nPost;
   
   divPost.attr("id", strCont + nPost);
   divPost.appendTo($("#" + strCont));
   
   for(var i=Entry.link.length-1;i >= 0; i--) {
    if(Entry.link[i].rel == "alternate") {
     $(idPost + " h3 a").text(Entry.title.$t);
      $(idPost + " h3 a").attr("href", "javascript:void();"); //Entry.link[i].href);
     divPost.click({ href: Entry.link[i].href}, jsPostClickNav);
	  break;
	 }
   }
//IE doesn't like the string format, so extract year, month, day
   var strDate = Entry.published.$t;
   strDate = strDate.substr(0, strDate.search("T"));
   strDate = strDate.split("-");

   var datePost = new Date(strDate[0], strDate[1]-1, strDate[2]);
   $(idPost + " h4").text( datePost.toLocaleDateString());

   $(idPost + " .js-post-body").append($(Entry.content.$t).html());//Append Script cleaned content
   $(idPost + " .blogger-post-footer").remove();
			
   if (Entry.media$thumbnail != undefined) {
	  $(idPost + " .js-post-thumb img").attr("src", Entry.media$thumbnail.url);
   }
   else if($(idPost + " .js-post-body img:first").length > 0) {		  
	  $(idPost + " .js-post-thumb img").attr("src", $(idPost + " .js-post-body img:first").attr("src"));			  		  	
   }
   else {
	  $(idPost + " .js-post-thumb").hide();
   }	
   
   if(This.strLayout != "") {
     $(idPost).addClass("js-post-" + This.strLayout);
     $(idPost).removeClass("js-post-template");
	 if(This.strLayout == "center") {
	 	$(idPost + " h3").insertBefore(idPost + " .js-post-thumb");
	 }
    else if(This.strLayout == "bottom") {
	 	$(idPost + " .js-post-thumb").insertAfter(idPost + " h4");
	 }
   }
   if(This.nMaxLength <= 0) {
   	 $(idPost + " .js-post-body").html();
   	 $(idPost + " .js-post-body").hide();	
   }
   else {
    $(idPost + " .js-post-body").html($(Entry.content.$t).text().substr(0,This.nMaxLength) + "...");
   }
   divPost.show();
 }
}